from freeswitch import *
import datetime, time, md5, os, sys

from pytz import timezone


class FrontdoorMain:

    def __init__(self, session):
        self.session = session

    def bridge2endpoint(self):
        self.session.execute("set", "call_timeout=30")
        self.session.execute("set", "continue_on_fail=true")
        self.session.execute("set", "hangup_after_bridge=true")
        self.session.execute("set", "ringback=%(2000,4000,440.0,480.0)")
        mycellphone = "12023334444"
        mynetphone = "foo@bar.com"
        self.session.execute("bridge",
                             "sofia/gateway/acme/%s, sofia/default/%s" %
                             (mycellphone, mynetphone))
        self.session.execute("set", "hangup_after_bridge=true")

    def internal_access(self):

        caller_id = self.session.getVariable("caller_id_number")
        console_log("info", "caller id:: %s" % caller_id)

        if caller_id in ['12021112222', '4511112222']:
            logged_in = True
        else:
            # could prompt them and compare input against a db
            # and set logged_in variable with the result
            pass

        if not logged_in:
            self.speak("Goodbye")
            return

        # 1. enter a local extension
        # 2. dial out of gateway
        # 3. listen to voicemail
        thing2say = "Enter_extension_followed_by_the_pound_sign.wav"
        in_rslt = self.session.playAndGetDigits(1, 15, 3, 15000,
                                                "*#", thing2say,
                                                "", "")[1]
        if in_rslt:
            self.session.execute("execute_extension", in_rslt)

    def main(self):
        thing2say = "Hello_Connecting_your_call_please_hold.wav"
        in_rslt = self.session.playAndGetDigits(1, 1, 1, 1000,
                                                "*#", thing2say,
                                                "", "")[1]
        if in_rslt:
            if int(in_rslt) == 0:
                return self.internal_access()

        tz_name = "America/Los_Angeles"
        tz = timezone(tz_name)
        now = datetime.datetime.now(tz)
        if now.hour >= 7 and now.hour <= 22:
            self.bridge2endpoint()
        else:
            self.session.answer()
            # play a warning saying only continue if emergency,
            # press 1 to continue
            thing2say = "You_have_called_after_hours.wav"
            in_rslt = self.session.playAndGetDigits(1, 1, 1, 5000,
                                                    "*#", thing2say,
                                                    "", "")[1]
            bridgecall = False
            if in_rslt:
                # if they press 1, let them through ..
                if int(in_rslt) == 1:
                    bridgecall = True

            if bridgecall:
                self.bridge2endpoint()
            else:
        # transfer to voicemail..
        pass


def handler(uuid):
    if not uuid:
        console_log("info", "\nNo uuid given\n")
        return
    session = PySession(uuid)
    frontdoormain = FrontdoorMain(session)
    frontdoormain.main()